<?php

namespace App\Filament\Resources;

use App\Filament\Resources\UserResource\Pages;
use App\Models\User;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Actions\Action;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\FileUpload;
use Filament\Tables\Actions\ActionGroup;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Actions\ViewAction;
use Filament\Tables\Actions\DeleteAction;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;

class UserResource extends Resource
{
    protected static ?string $model = User::class;

    protected static ?string $navigationIcon = 'heroicon-o-user-plus';

    protected static ?int $navigationSort = 1;

    // 导航项分组翻译
    public static function getNavigationGroup(): ?string
    {
        return __('menu.power_group.name');
    }

    // 菜单标题导航翻译
    public static function getNavigationLabel(): string
    {
        return __('menu.power_group.user');
    }

    // 面包屑导航翻译
    public static function getModelLabel(): string
    {
        return __('menu.power_group.user');
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                FileUpload::make('avatar')
                    ->label(__('user.Avatar'))
                    ->avatar()
                    ->getUploadedFileNameForStorageUsing(
                        function (TemporaryUploadedFile $file) {
                            return (string)str('avatar/' . date('Ymd') . '/' . $file->getClientOriginalName());
                        }
                    ),
                TextInput::make('name')->label(__('user.Name'))->required(),
                TextInput::make('mobile')->label(__('user.Mobile'))->maxLength(11),
                TextInput::make('email')->label(__('user.Email')),
                TextInput::make('username')->label(__('user.Username'))->required(),
                TextInput::make('password')
                    ->label(__('user.Password'))
                    ->password()
                    ->revealable()
                    ->helperText('为空则表示不修改密码')
                    ->minLength(6)
                    ->maxLength(32)
                    ->required(function (string $operation) {
                        return $operation == 'create';
                    }),
                Select::make('roles')
                    ->label(__('user.Roles'))
                    ->relationship('roles', 'name')
                    ->multiple()
                    ->preload()
                    ->searchable()
                    ->required(),
            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                ImageColumn::make('avatar')->label(__('user.Avatar'))->circular()->alignCenter(),
                TextColumn::make('name')->label(__('user.Name'))->copyable()->alignCenter(),
                TextColumn::make('username')->label(__('user.Username'))->copyable()->badge()->color('success')->alignCenter(),
                TextColumn::make('mobile')->label(__('user.Mobile'))->copyable()->alignCenter(),
                TextColumn::make('roles.name')
                    ->label(__('user.Roles'))
                    ->listWithLineBreaks()
                    ->badge()
                    ->separator()
                    ->alignCenter(),
            ])
            ->filters([
                //
            ])
            ->actions([
                ActionGroup::make([
                    ViewAction::make(),
                    EditAction::make()->mutateFormDataUsing(function (array $data): array {
                        if (is_null($data['password'])) {
                            unset($data['password']);
                        }
                        return $data;
                    }),
                    DeleteAction::make(),
                ]),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListUsers::route('/'),
        ];
    }
}
